 <?php
require_once('./inc/database.php');
 db_connect();
 class user 
 {
  private $user_id;
  public $user_name;
  public $user_password;
  public $user_customer;
  public $user_mail;
  public $role_type; 
  public $user_active;
  public $send_mail;
  public $Fname;
  public $Lname;
  public $pon;
  public $mobile;
  public $Employee_Friilnsr;
  public $Portfolio;
  /* constractor */
  public function __construct()
  {
	$this->user_name="";
	$this->user_password="";
	$this->user_customer="";
	$this->user_mail="";
	$this->role_type="";
	$this->user_active=1;
	$this->send_mail=0;
	$this->Fname="";
	$this->Lname="";
	$this->pon="";
	$this->mobile="";
	$this->Employee_Friilnsr="";
	$this->Portfolio="";
	  
	 }
 
	 /******* method*******/
	public function getuser_id()
	{ if($this->user_id!=null)
	  {
	    return $this->user_id;	
	  }
	}
	 public function insert()
	  {
	  	 $stringinsert=sprintf("INSERT INTO `user`( `user_name`, `user_password`, `user_customer`, `user_mail`,
		  `role_type`, `user_active`,`send_mail`,`Fname`,`Lname`,`pon`,`mobile`,`Employee_Friilnsr`,`Portfolio`) 
		    VALUES ('$this->user_name','$this->user_password','$this->user_customer',
		  '$this->user_mail','$this->role_type',$this->user_active,$this->send_mail,
		  '$this->Fname','$this->Lname','$this->pon','$this->mobile','$this->Employee_Friilnsr','$this->Portfolio')");
		 $queryinsert=mysql_query($stringinsert)or die (mysql_error().' '.$stringinsert);
		/* $stringgetuser_id=sprintf("SELECT * FROM `user` WHERE `user_name`='$this->user_name'");
		 $querygetuser_id=mysql_query($stringgetuser_id)or die (mysql_error().' '.$stringgetuser_id);
		 $row=mysql_fetch_assoc($querygetuser_id);
		*/ 
		 $this->user_id=mysql_insert_id();
		 }
		 
	 public function update()
	 {
		 if($this->user_id!=null)
		 {
			$stringupdate = sprintf("UPDATE `user` SET 
			`user_name`='$this->user_name',
			`user_password`='$this->user_password'
			,`user_customer`='$this->user_customer',
			`user_mail`='$this->user_mail',
			`role_type`='$this->role_type',
			`user_active`=$this->user_active,
			`send_mail`=$this->send_mail,
			`Fname`='$this->Fname',
			`Lname`='$this->Lname',
			`pon`='$this->pon',
			`mobile`='$this->mobile',
			`Employee_Friilnsr`='$this->Employee_Friilnsr',
			 `Portfolio`='$this->Portfolio'
			 WHERE `user_id`= '$this->user_id'");	 
			$queryupdateuser= mysql_query($stringupdate) or die (mysql_error().' '.$stringupdate);
			return true;
		 }
		return false; 
	 }
	 public function copy_user($user)/*   הפונקציה מעתיקה את כל המשתנים של המשתנה החיצוני למשתנה שלנו	*/
 	 {
	 	$this->user_name=$user->user_name;
		$this->user_password=$user->user_password;
		$this->user_customer=$user->user_customer;
		$this->user_mail=$user->user_mail;
		$this->role_type=$user->role_type;
		$this->user_active=$user->user_active;
		$this->send_mail=$user->send_mail;
		$this->Fname=$user->Fname;
		$this->Lname=$user->Lname;
		$this->pon=$user->pon;
		$this->mobile=$user->mobile;
		$this->Employee_Friilnsr=$user->Employee_Friilnsr;
		$this->Portfolio=$user->Portfolio;
	 }
	 public function getby_name($user_name)
	 	{  
			$stringgetby_id=sprintf("SELECT * FROM user WHERE user_name='$user_name'");
			$querygetby_id=mysql_query($stringgetby_id) or die(mysql_error().' '.$stringgetby_id);
			$row=mysql_fetch_assoc($querygetby_id);
			if(mysql_affected_rows()>0)
			{
				$this->user_id=$row['user_id'];
				$this->user_name=$row['user_name'];
				$this->user_password=$row['user_password'];
				$this->user_customer=$row['user_customer'];
				$this->user_mail=$row['user_mail'];
				$this->role_type=$row['role_type'];
				$this->user_active=$row['user_active'];
				$this->send_mail=$row['send_mail'];
				$this->Fname=$row['Fname'];
				$this->Lname=$row['Lname'];
				$this->pon=$row['pon'];
				$this->mobile=$row['mobile'];
				$this->Employee_Friilnsr=$row['Employee_Friilnsr'];
				$this->Portfolio=$row['Portfolio'];		
			}
		 	
		}
		 public function getby_id($user_id)
	 	{
			$stringgetby_id=sprintf("SELECT * FROM user WHERE user_id='$user_id'");
			$querygetby_id=mysql_query($stringgetby_id) or die(mysql_error().'  '.$stringgetby_id);
			$row=mysql_fetch_assoc($querygetby_id);
			if(mysql_affected_rows()>0)
			{
				$this->user_id=$row['user_id'];
				$this->user_name=$row['user_name'];
				$this->user_password=$row['user_password'];
				$this->user_customer=$row['user_customer'];
				$this->user_mail=$row['user_mail'];
				$this->role_type=$row['role_type'];
				$this->user_active=$row['user_active'];
				$this->send_mail=$row['send_mail'];	
				$this->Fname=$row['Fname'];
				$this->Lname=$row['Lname'];
				$this->pon=$row['pon'];
				$this->mobile=$row['mobile'];		
				$this->Employee_Friilnsr=$row['Employee_Friilnsr'];
				$this->Portfolio=$row['Portfolio'];		

			}
		 	
		}
	public static function isuser($user_name)//מחזיר אמת האם יש משתמש כזה במערכת ושקר אם אין 
		{
			$stringisuser=sprintf("SELECT * FROM `user` WHERE  `user_name`='$user_name'");
			$queryisuser=mysql_query($stringisuser) or die(mysql_error().' '.$stringisuser);
			if(mysql_affected_rows()>0)
			{
				return true;
			}
			else 
			{
				return 0;
			}
			
		}
	public function get_proj_permissions()// מחזיר מערך של פרויקטיים לפי ההרשאה
		{  if($this->user_id!="" || $this->user_id!=null)
			  {
			   $stringproPERM=sprintf("SELECT * FROM `permissions` WHERE `user_id`=$this->user_id and Active=1");
			   $queryproPERM=mysql_query($stringproPERM) or die (mysql_error().' ' .$stringproPERM);	
			   $arr=array();
			  while($row=mysql_fetch_assoc($queryproPERM))
			  {
				 array_push($arr,$row['project_id']);
					 
			  }
			  return $arr;
			  }
		  return false;
		}
		public function get_proj_unpermissions()// מחזיר מערך של פרויקטיים שלא  בההרשאה של המשתמש 
		/* בפונקציה הזאת רק התכנתן והאיש בדיקה משתמשים בה */
 		{
			
		if($this->user_id!="" || $this->user_id!=null)
			  {
			   $stringproPERM=sprintf("SELECT  * FROM `permissions` WHERE `user_id` <>$this->user_id
					AND `project_id` NOT IN (SELECT `project_id` FROM `permissions` WHERE `user_id` = $this->user_id)
					AND Active=1
	         	    GROUP BY project_id
					ORDER BY `permissions`.`project_id` ASC");
					
			   $queryproPERM=mysql_query($stringproPERM) or die (mysql_error().' ' .$stringproPERM);	
			   $arr=array();
			  while($row=mysql_fetch_assoc($queryproPERM))
			  {
				 array_push($arr,$row['project_id']);
					 
			  }
			  return $arr;
			  }
		  return false;
		}
		
	public static function get_name($user_id)// מחזירה את השם של המשתמש 
		{
		   $strintname=sprintf("SELECT  `user_name` FROM `user` WHERE  `user_id`=$user_id");
		   $queryname=mysql_query($strintname) or die (mysql_error().' '.$strintname);
		   if(mysql_affected_rows()>0)
		       {
				    $row=mysql_fetch_assoc($queryname);
					 return $row['user_name'];
			  }	 
			  else return false;
		}
	public static function getby_customer($id_customer)
		{
			
		 	$st=sprintf("SELECT * FROM `user` WHERE `user_customer`=$id_customer AND user_active=1");
			$query=mysql_query($st) or die(mysql_error().' '.$st);
			 $arr=array();
			  while($row=mysql_fetch_assoc($query))
			  { 
				  $temp=new user();
				  $temp->getby_id($row['user_id']); 
				 array_push($arr,$temp);
					 
			  }
			  return $arr;
		
		  return false;
			
		}
	public static function getby_customer_in_role_type($id_customer,$role_type)
		{
			
		 	$st=sprintf("SELECT * FROM `user` WHERE `user_customer`=$id_customer AND `role_type`=$role_type AND user_active=1");
			$query=mysql_query($st) or die(mysql_error().' '.$st);
			 $arr=array();
			  while($row=mysql_fetch_assoc($query))
			  { 
				  $temp=new user();
				  $temp->getby_id($row['user_id']); 
				 array_push($arr,$temp);
					 
			  }
			  return $arr;
		
		  return false;
			
		}	
	public static function get_role_type_byid($user_id)//מחזיר את סוג תפקיד 
		{
		  $st="SELECT role_type,user_name FROM user WHERE user_id=$user_id";
		  $query=mysql_query($st)or die(mysql_error(). ' '.$st);
		   if(mysql_affected_rows()>0)
		    {
				$row=mysql_fetch_assoc($query);
			  return $row;		
			}
			else
			 {
			    return "null";	 
			 }
		 
		}
		
		
	public static function get_user_contect($id_customer)//מחזיר את הפרטים של האיש קשר של החברה 
		{
		   $st=sprintf("SELECT * FROM `user` WHERE  `user_customer`=$id_customer and `role_type`=4");
		   $query=mysql_query($st)or die (mysql_error().' '.$st);
		   if(mysql_affected_rows()>0)
		   	{
				$row=mysql_fetch_assoc($query);
				$temp=new user();
				$temp->getby_id($row['user_id']);
				return $temp;
			} 
			else 
				{
				 return 0;	
				}
		   	
			
		} 
public static function get_user_contects($id_customer)//מחזיר את הפרטים של האנשי קשר של החברה 
		{
		   $st=sprintf("SELECT * FROM `user` WHERE  `user_customer`=$id_customer and `role_type`=4");
		   $query=mysql_query($st)or die (mysql_error().' '.$st);
		   if(mysql_affected_rows()>0)
		   	{   $arr=array();
				while($row=mysql_fetch_assoc($query))
				 {
					$temp=new user();
					$temp->getby_id($row['user_id']);
					array_push($arr,$temp);
				 }
			return $arr;	 
			} 
			else 
				{
				 return 0;	
				}
		   	
			
		} 

public static function get_user_by_type($role_type)//מחזיר את כל המשתמשים לפי סוג תפקיד
{
	$st=sprintf("SELECT * FROM `user` WHERE  `role_type`=$role_type");
		   $query=mysql_query($st)or die (mysql_error().' '.$st);
		   if(mysql_affected_rows()>0)
		   	{ $arr=array();
				while($row=mysql_fetch_assoc($query))
				{
					$temp=new user();
					$temp->getby_id($row['user_id']);
				  array_push($arr,$temp);
				}
				return $arr;
			} 
		else 
			{
			   return 0;	
			}
  
}
public static function GetNameOfRoletype($IdRole)
{
	$st=sprintf("SELECT * FROM `role_type` WHERE `role_type_id`=$IdRole");
		   $query=mysql_query($st)or die (mysql_error().' '.$st);
		   if(mysql_affected_rows()>0)
		   	{ 
				$row=mysql_fetch_object($query);
				return $row->role_type_desc;	
			}
				
			 
		else 
			{
			   return 0;	
			}
	
}
public static function GetFormNewUser($IdCustomer,$RoleType)
{?>
	 <form id="form_new_user" class="admin_form" name="form_new_user" method="post" action="update.php">
     	<h3>יצירת משתמש חדש</h3>
         <input id="hid_new_user" name="hid_new_user" type="hidden"  />
           <fieldset>
            <legend>פרטי משתמש:</legend>
            <div class="form_set">
           	    <label>שם משתמש:</label>
            	<input id="name" name="name" type="text"  />
            </div>
            <div class="form_set">
            	<label>סיסמא:</label>
            	<input id="password" name="password" type="text"  />
            </div>
            <div class="form_set">
            	<label>אימות סיסמא:</label>
            	<input id="password2" name="password2" type="text"  />
            </div>
           </fieldset>
            <fieldset>
           <div class="form_set">
           <label>לקוח:</label>
            <select id="customer" name="customer">
              <option value="none" >לקוח:</option>
              <?php  get_customer();?>
            </select>
           </div>
           <div class="form_set">
            <label>אימייל:</label>
            <input id="mail" name="mail" type="text"  />
           </div>
           <div class="form_set">
           <label>תפקיד:</label>
            <select id="role_type" name="role_type">
            <?php get_role_type() ;?>
            </select>
           </div>
           <div class="form_set">
            <label>שם פרטי:</label>
            <input id="Fname" name="Fname" type="text"  />
           </div>
           <div class="form_set">
            <label>שם משפחה:</label>
            <input id="Lname" name="Lname" type="text"  />
           </div>
            <div class="form_set">
            <label>טלפון:</label>
            <input id="pon" name="pon" type="text"  />
           </div>
            <div class="form_set">
            <label>סלולרי:</label>
            <input id="mobile" name="mobile" type="text"  />
           </div>
           <div class="form_set">
           <div id="div_send_mail">
            <label>שליחת מייל לכל הודעה חדשה:</label>
            <input id="send_mail" name="send_mail" type="checkbox" value="1" checked="checked" />
           </div>   
           </div>      
           </fieldset>
           
          
          <input id="btn_form_new_user" name="btn_form_new_user" type="button" value="שלח" />
          </form>
          
           <div id="error" ><li class="red">* עליך למלא את השדות:</li></div>
          </div>
           <script src="inc/js/jquery.validate.js" type="text/javascript"></script>
       <script>
			 jQuery.validator.addMethod("selectNone", 
			  function(value, element) { 
				if (element.value == "none") 
				{ 
				  return false; 
				} 
				else return true; 
			  }, 
		  "Please select an option." 
		  ); 
	$(document).ready(function(e) {
		 
		  $('#role_type').change(function(){
					  if($('#role_type').val()==4 || $('#role_type').val()==1)
					  	{
						  $('#div_send_mail').css('display','block');	
						}
						else 
						  {
						     $('#div_send_mail').css('display','none');		  
					      }
					 });	
      
<?php
    }
 }
 
?>